/**
 * Author:beyondboy
 * Gmail:xuguoli.scau@gmail.com
 * Date: 2015-04-27
 * Time: 14:56
 */
package com.scau.beyondboy.criminalintent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;

import java.util.ArrayList;
import java.util.UUID;
public class CrimePagerActivity extends FragmentActivity
{
    private static final String TAG ="CrimePagerActivity" ;
    private ViewPager mViewPager;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        Log.i(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        mViewPager=new ViewPager(this);
        mViewPager.setId(R.id.viewPager);
        final ArrayList<Crime> crimes=CrimeLab.get(this).getCrimes();
        FragmentManager manager=getSupportFragmentManager();
        mViewPager.setAdapter(new FragmentStatePagerAdapter(manager)
        {
            @Override
            public Fragment getItem(int position)
            {
                return CrimeFragment.newInstance(crimes.get(position).getId());
            }
            @Override
            public int getCount()
            {
                return crimes.size();
            }
        });
        final UUID crimeId=(UUID)getIntent().getSerializableExtra(CrimeFragment.EXTRA_CRIME_ID);
        int i=crimes.indexOf(CrimeLab.get(this).getCrime(crimeId));
        mViewPager.setCurrentItem(i);
        setContentView(mViewPager);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
            {
                Log.i(TAG,"onPageScrolled");
            }
            /**
             * 设置被点击的标题
             * @param position
             */
            @Override
            public void onPageSelected(int position)
            {
                Log.i(TAG,"onPageSelected");
                Crime crime=crimes.get(position);
                if(crime.getTitle()!=null)
                {
                    setTitle(crime.getTitle());
                }
            }
            @Override
            public void onPageScrollStateChanged(int state)
            {
                Log.i(TAG,"onPageScrollStateChanged");
            }
        });
    }
}
